package com.xdw.vuebackdemo.service.impl;

import com.xdw.vuebackdemo.mapper.RoleMapper;
import com.xdw.vuebackdemo.model.Permission;
import com.xdw.vuebackdemo.model.UserRole;
import com.xdw.vuebackdemo.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.Set;
/**
 * @author xiadewang
 */
@Service
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    RoleMapper roleMapper;
    @Override
    public List<String> getPermissionNameListByUserId(long userId) {
        Set<Permission> permissionList = getPermissonListByUserId(userId);
        if(permissionList!=null && permissionList.size()>0){
            List<String> permissionNameList = new LinkedList<>();
            for(Permission permission:permissionList){
                permissionNameList.add(permission.getName());
            }
            return permissionNameList;
        }
        return null;
    }

    @Override
    public Set<Permission> getPermissonListByUserId(long userId) {
        UserRole userRole = roleMapper.findRolesByUserId(userId);
        Set<Permission> permissionList = null;
        if(userRole!=null && userRole.getRoles()!=null && userRole.getRoles().size()>0){
            permissionList = roleMapper.getPemissionsByListRole(userRole.getRoles());
        }
        return permissionList;
    }
}
